package br.elsa.survey.domain.questions;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.validation.constraints.Min;

/**
 * JPA {@link Entity} that abstracts a checkbox question that allows the user
 * select multiple options as answer
 * 
 * @author diego
 * @since 2010-10-07
 */
@Entity
@Table(name = "checkbox_questions", schema = "survey")
@DiscriminatorValue("CHECKBOX_QUESTION")
public class CheckboxQuestion extends MultipleChoiceQuestion<CheckboxOption> {

	private static final long serialVersionUID = 8567237085387137660L;

	/**
	 * The MIN number of options that <b>must</b> be chosen
	 */
	@Min(value = 1, message = "The MIN number of chosen options must be greater than 1")
	private int minNumberChosenOptions;

	/**
	 * Default constructor
	 * 
	 * <p>
	 * Initializes an instance with the {@link CheckboxQuestion#nullableAnswer}
	 * value equals to false, and the
	 * {@link CheckboxQuestion#minNumberChosenOptions} equals to 1
	 * </p>
	 */
	public CheckboxQuestion() {

		this.setMinNumberChosenOptions(1);
	}

	/**
	 * Creates a new empty {@link CheckboxOption} and add it to the
	 * {@code options} list
	 * 
	 * <p>
	 * The relation between {@link CheckboxQuestion} and {@link CheckboxOption}
	 * is composition and the responsibility of creating a new
	 * {@link CheckboxOption} belongs to the {@link CheckboxQuestion}
	 * </p>
	 */
	public void createNewEmptyOption() {

		CheckboxOption option = new CheckboxOption();
		option.setQuestion(this);

		options.add(option);
	}

	/**
	 * Sets the MIN number of options that <b>must</b> be selected
	 * 
	 * @param minNumberChosenOptions
	 *            the MIN number of options to be selected
	 */
	public void setMinNumberChosenOptions(int minNumberChosenOptions) {
		this.minNumberChosenOptions = minNumberChosenOptions;
	}

	/**
	 * Gets the MIN number of options to be selected
	 * 
	 * @return MIN number of options to be selected
	 */
	public int getMinNumberChosenOptions() {
		return minNumberChosenOptions;
	}

}
